package com.etc.service;

import static com.etc.common.ThisSystemutil.*;

import com.etc.common.ThisSystemExcption;
import com.etc.dao.SysUserMapper;
import com.etc.entity.SysUser;
import com.etc.entity.SysUserExample;
import com.etc.entity.SysUserKey;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangcc
 * @version V1.0
 * @Description:
 * @date 2018/4/4  9:41
 */
@Service
public class SysUserService {

    private static final Logger log = LoggerFactory.getLogger(SysUserService.class);

    @Resource
    private SysUserMapper sysUserMapper;


    public SysUser findById(String id){
        SysUserKey sysUserKey = new SysUserKey();
        sysUserKey.setId(Integer.valueOf(id));
        return  sysUserMapper.selectByPrimaryKey(sysUserKey);
    }


    public Integer chognzhi(String id ,String number){
        Integer groupAnditId = null;
        try {
            SysUserKey sysUser = new SysUserKey();
            sysUser.setId(Integer.valueOf(id));
            SysUser sysUser1 = sysUserMapper.selectByPrimaryKey(sysUser);


            SysUserExample sysUserExample = new SysUserExample();
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            criteria.andIdEqualTo(sysUser1.getId());
            sysUser1.setMony(String.valueOf(Integer.valueOf(sysUser1.getMony())+Integer.valueOf(number)));
            groupAnditId = sysUserMapper.updateByExample(sysUser1, sysUserExample);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return groupAnditId;
    }



    /**
     * 添加用户
     * @param user
     * @return
     */
    public Integer add (SysUser user){
        user.setRole("2");
        user.setSaveDate(String.valueOf(System.currentTimeMillis()));
        user.setMember("1");
        user.setMony("0");
        return sysUserMapper.insert(user);
    }

    /**
     * 查询用户
     * @param
     * @return
     */
    public SysUser findSysUser(String username,String password){

         $("用户名不能为空",username);
         $("密码不能为空",password);

        List<SysUser> sysUsersList = new ArrayList<SysUser>();
            SysUserExample sys = new SysUserExample();
            SysUserExample.Criteria criteria = sys.createCriteria();
            criteria.andUsernameEqualTo(username);
            sysUsersList = sysUserMapper.selectByExample(sys);
            if (sysUsersList ==null || sysUsersList.isEmpty()){
                throw new ThisSystemExcption("不存在的用户");
            }
            if(!sysUsersList.get(0).getPassword().equals(password)){
                throw new ThisSystemExcption("密码错误");
            }

        return sysUsersList.get(0);
    }


    /**
     * 查询所有用户
     * @return
     */
    public PageInfo findByRoleAll(int page1) {

        PageHelper.startPage(page1, 10);
        List<SysUser> sysUsers = new ArrayList<SysUser>();

        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andRoleEqualTo("2");

        sysUsers = sysUserMapper.selectByExample(sysUserExample);

        PageInfo page = new PageInfo(sysUsers);
        return page;
    }


    /**
     * 更新用户信息
     * @return
     */
    public Integer findByRoleAlldf(SysUser sysUser) {
        Integer groupAnditId = null;
        if (sysUser == null) {
            return groupAnditId;
        }
        try {
            SysUserExample sysUserExample = new SysUserExample();
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            criteria.andIdEqualTo(sysUser.getId());

            groupAnditId = sysUserMapper.updateByExample(sysUser, sysUserExample);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return groupAnditId;
    }

    /**
     * 更新管理员信息
     * @return
     */
    public Integer updateUser(SysUser sysUser) {
        Integer groupAnditId = null;
        if (sysUser == null) {
            return groupAnditId;
        }
        try {
            SysUserExample sysUserExample = new SysUserExample();
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            criteria.andIdEqualTo(sysUser.getId());
            sysUser.setSaveDate(String.valueOf(System.currentTimeMillis()));
            sysUser.setRole("1");
            groupAnditId = sysUserMapper.updateByExample(sysUser, sysUserExample);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return groupAnditId;
    }
    /**
     * 更新管理员信息
     * @return
     */
    public SysUser selectUsername(String username) {
        SysUser groupAnditId = null;
        if (username == null) {
            return groupAnditId;
        }
        try {
            SysUserExample sysUserExample = new SysUserExample();
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            criteria.andNameEqualTo(username);
            criteria.andRoleEqualTo("2");
            List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
            if(sysUsers != null && !sysUsers.isEmpty()){

                groupAnditId = sysUsers.get(0);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return groupAnditId;
    }
}
